import {CSV} from "db://assets/scripts/common/CSV";
import {Skill} from "db://assets/scripts/data/model/Skill";
import {SkillField} from "db://assets/scripts/data/db/SkillField";
import {SkillType} from "db://assets/scripts/data/model/SkillType";
import {SkillWarningType} from "db://assets/scripts/data/model/SkillWarningType";
import {Vec3Converter} from "db://assets/scripts/data/converter/Vec3Converter";

/**
 * 技能转换器
 * public convertFromVector: (V) -> T
 * public convertToVector: (T) -> V
 */
export class SkillConverter {
    /**
     * 禁止创建对象
     */
    private constructor() {
    }

    /**
     * 转数组
     *
     * @param csv CSV
     */
    public static convertFromCSV(csv: CSV): Skill[] {
        let result: Skill[] = [];
        csv.datas.forEach((row) => {
            result.push(this.convertFromRow(row));
        });
        return result;
    }

    /**
     * 转对象
     *
     * @param row CSV行数据
     */
    public static convertFromRow(row: string[]): Skill {
        return {
            id: row[SkillField.ID],
            name: row[SkillField.NAME],
            description: row[SkillField.DESCRIPTION],
            icon: row[SkillField.ICON],
            resourceName: row[SkillField.RESOURCE_NAME],
            level: Number(row[SkillField.LEVEL]),
            value: row[SkillField.VALUE],
            type: this.parseType(row[SkillField.TYPE]),
            warning: this.parseWarning(row[SkillField.WARNING]),
            startPosition: Vec3Converter.convertFromString(row[SkillField.START_POSITION]),
            speed: Number(row[SkillField.SPEED]),
            permeable: Boolean(row[SkillField.PERMEABLE]),
            price: Number(row[SkillField.PRICE]),
        };
    }

    /**
     * 解析技能类型
     *
     * @param type 技能类型字符串
     */
    private static parseType(type: string): SkillType {
        switch (type) {
            case SkillType.SINGLE.toString():
                return SkillType.SINGLE;
            case SkillType.THROW.toString():
                return SkillType.THROW;
            case SkillType.RANGE.toString():
                return SkillType.RANGE;
            case SkillType.SCATTER.toString():
                return SkillType.SCATTER;
            case SkillType.S.toString():
                return SkillType.S;
            case SkillType.FALL.toString():
                return SkillType.FALL;
            case SkillType.HEXAGON.toString():
                return SkillType.HEXAGON;
        }
    }

    /**
     * 解析技能预警
     *
     * @param warning 技能预警字符串
     */
    private static parseWarning(warning: string): SkillWarningType {
        switch (warning) {
            case SkillWarningType.WARNING_LINE.toString():
                return SkillWarningType.WARNING_LINE;
            case SkillWarningType.WARNING_STRIP.toString():
                return SkillWarningType.WARNING_STRIP;
            case SkillWarningType.WARNING_CIRCLE.toString():
                return SkillWarningType.WARNING_CIRCLE;
        }
    }
}